package view;

import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import view.CenterTabbedPane.EBenchmarkTab;

/**
 * The {@code EBenchmarkPanel} is the type of JPanel to choose in this
 * application when a {@code JPanel} should be created as a tab in the main
 * {@code JTabbedPane}. So, every {@code JPanel} in the main {@code JTabbedPane}
 * is an {@code EBenchmarkPanel}.
 * @author Oliver Eckstein
 *
 */
public abstract class EBenchmarkPanel
    extends JPanel implements IObserver {
    
    private static final long serialVersionUID = 1L;    
    
    /**
     * The type of {@code EBenchmarkTab} of this panel.
     */
    protected EBenchmarkTab tab;
    
    /**
     * A logo that allows the user to identify the type of panel.
     */
    protected JLabel logo;
    
    /**
     * To create a new {@code EBenchmarkPanel}.
     * @param logoPath a path to an optional logo. If the path is {@code null}
     * a standard logo is used.
     * @param tab the panel's type of tab.
     */
    public EBenchmarkPanel(String logoPath, EBenchmarkTab tab) {
        logo = createImageLabel(logoPath);
        this.tab = tab;
        this.setErrorStatus(false);
        this.setActive(true);
    }
    
    /**
     * To set the <b>error status</b> of this panel.
     * @param errorStatus error status.
     */
    public void setErrorStatus(boolean errorStatus) {
        this.putClientProperty("errorStatus", errorStatus);
    }
    
    /**
     * To get the error status of this panel.
     * @return error status.
     */
    public boolean getErrorStatus() {
        if (this.getClientProperty("errorStatus").equals(true)) {
            return true;
        } else {
            return false;
        }        
    }
    
    /**
     * To set the active status of this tab.
     * @param status active status of this tab.
     */
    public void setActive(boolean status) {
        this.putClientProperty("isActive", status);
    }
    
    /**
     * To get the active status of this tab.
     * @return active status of this tab.
     */
    public boolean isActive() {
        if (this.getClientProperty("isActive").equals(true)) {
            return true;
        } else {
            return false;
        }  
    }
    
    @Override
    public String toString() {
        return "EBenchmarkPanel: '" + this.getName() + "'";
    }
    

    
    /**
     * Returns a BufferedImage.
     * @param path path of the image.
     * @return BufferedImage of the path.
     * @throws NullPointerException when path was invalid.
     */
    protected JLabel createImageLabel(String path) {
        if (path == null) {
        	path = "/img/logo.jpg";
        }
        URL resource = MainFrame.class.getResource(path);
        ImageIcon img = null;
        if(resource != null) {
            img = new ImageIcon(resource);                
        } else {
            System.err.println("Couldn't find file: " + path);            
        }        
        JLabel imageLabel = new JLabel(img);
        return imageLabel;
    }
    
    /**
     * To get the type of tab of this {@code EBenchmarkPanel}.
     * @return the tab.
     */
    public EBenchmarkTab getTab() {
    	return tab;
    }
}
